﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BI.CF.Model;
using System.Data;
using System.Data.SqlClient;
using BI.CF.IDAL;

namespace BI.CF.DAL
{
    public class UserDAL : UserIDAL
    {
        /// <summary>  
        /// 用户注册
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public bool User_Register(string name, string pwd)
        {

            
            SqlParameter[] parameter = {
                                         new SqlParameter("@UserName",SqlDbType.VarChar,50),
                                         new SqlParameter("@UserPwd",SqlDbType.VarChar,50),
                                        
                                     };
            parameter[0].Value = name;
            parameter[1].Value = pwd;
           
            return DbHelperSQL.RunProcedureReturnBool("PROC_UserRegister", parameter);
        }
            
        /// <summary>
        /// 注册用户是否重复
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns></returns>
        public int User_RegisterRepeat(string name)
        {
            SqlParameter[] parameter = {
                                           new SqlParameter("@name",SqlDbType.VarChar,50)
                                       };
            parameter[0].Value = name;
            SqlDataReader read = DbHelperSQL.RunProcedure("PROC_SelectUserName", parameter);
            UserrsModel umodel = new UserrsModel();
            while(read.Read())
            {
                umodel.UserName = read["UserName"].ToString();
            }
            if(umodel.UserName == null)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        //public UserrsModel User_LoginState(int Uid)
        //{
        //    SqlParameter[] parameter = {
        //                                   new SqlParameter("@Uid",SqlDbType.Int)
        //                               };
        //    parameter[0].Value = Uid;
        //    SqlDataReader read = DbHelperSQL.RunProcedure("PROC_LoginState", parameter);
        //    UserrsModel umodel = new UserrsModel();
        //    while(read.Read())
        //    {
        //        umodel.UserId = Convert.ToInt32(read["UserId"].ToString());
        //        umodel.UserName = read["UserName"].ToString();
        //        umodel.UserPwd = read["UserPwd"].ToString();
        //    }
        //    return umodel;

        //}

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public UserrsModel User_Login(string name, string pwd)
        {
            SqlParameter[] parameter = {
                                          new SqlParameter("@name",SqlDbType.VarChar,50),
                                          new SqlParameter("@pwd",SqlDbType.VarChar,50)
                                     };
            parameter[0].Value = name;
            parameter[1].Value = pwd;
            //DataSet ds = DbHelperSQL.RunProcedure("PROC_UserLogin", parameter, "db");
            SqlDataReader read = DbHelperSQL.RunProcedure("PROC_UserLogin", parameter);
            UserrsModel model = new UserrsModel();
            if (read.Read() == false)
            {
                return model;
            }
            else
            {
                model.UserId =Convert.ToInt32(read["UserId"]);
                model.UserName = read["UserName"].ToString();
                model.UserPwd = read["UserPwd"].ToString();
                model.HeadImage = read["HeadImage"].ToString();
                model.IdCard = read["IdCard"].ToString();
                model.UserSex =read["UserSex"].ToString();
                model.UserEmail = read["UserEmail"].ToString();
                model.UserPhone = read["UserPhone"].ToString();
                model.UserTel = read["UserTel"].ToString();
                model.MaritalState =read["MaritalState"].ToString();                
                model.UserrBcardMesId =read["UserrBcardMesId"].ToString();
                model.UserrPlaceOrigin = read["UserrPlaceOrigin"].ToString();
                model.UserrAddres = read["UserrAddres"].ToString();
                model.UserrRealName = read["UserrRealName"].ToString();
                model.UserrOccupation = read["UserrOccupation"].ToString();

                if (read["UserAge"].ToString() == "")//Age数据为空时给予默认值
                {
                    model.UserAge = 0;
                }
                else
                {
                    model.UserAge = int.Parse(read["UserAge"].ToString());
                }

                model.UserEducation = read["UserEducation"].ToString();

                if (read["Creditlimit"].ToString() == "")//Creditlimit数据为空时给予默认值
                {
                    model.Creditlimit = 0;
                }
                else
                {
                    model.Creditlimit = int.Parse(read["Creditlimit"].ToString());
                }

                if (read["UserMemlevelId"].ToString() == "")//UserMemlevelId数据为空时给予默认值
                {
                    model.UserMemlevelId = 0;
                }
                else
                {
                    model.UserMemlevelId = int.Parse(read["UserMemlevelId"].ToString());
                }

                if (read["PaymentPwd"] == null || read["PaymentPwd"].ToString() =="")//支付密码为空时赋予默认值
                {
                    model.PaymentPwd = 0;
                }
                else
                {
                    model.PaymentPwd =Convert.ToInt32 (read["PaymentPwd"].ToString());
                }
                    
                read.Close();
                return model;
            }
        }

        /// <summary>
        /// 根据借款编号查询借款人具体信息
        /// </summary>
        /// <returns></returns>
        public UserrsModel Get_OnlyUsers(int ID)
        {
            SqlParameter[] Paramter = { new SqlParameter("@MemLoanId", SqlDbType.Int) };
            Paramter[0].Value = ID;
            SqlDataReader dt = DbHelperSQL.RunProcedure("Proc_UsersSelect", Paramter);
            UserrsModel m = new UserrsModel();
            dt.Read();
           
                m.Creditlimit = int.Parse(dt["Creditlimit"].ToString());
                m.HeadImage = dt["HeadImage"].ToString();
                m.IdCard = dt["IdCard"].ToString();
           
                m.MIncome = decimal.Parse(dt["MIncome"].ToString());
                m.PaymentPwd = int.Parse(dt["PaymentPwd"].ToString());
                m.UserAge = int.Parse(dt["UserAge"].ToString());
                m.UserEducation = dt["UserEducation"].ToString();
                m.UserEmail = dt["UserEmail"].ToString();
                m.UserId = int.Parse(dt["UserId"].ToString());
                m.UserMemlevelId = int.Parse(dt["UserMemlevelId"].ToString());
                m.UserName = dt["UserName"].ToString();
                m.UserPhone = dt["UserPhone"].ToString();
                m.UserPwd = dt["UserPwd"].ToString();
                m.UserrAddres = dt["UserrAddres"].ToString();
                m.UserrBcardMesId = dt["UserrBcardMesId"].ToString();
                m.UserrOccupation = dt["UserrOccupation"].ToString();
                m.UserrPlaceOrigin = dt["UserrPlaceOrigin"].ToString();
                m.UserrRealName = dt["UserrRealName"].ToString();
                m.UserSex = dt["UserSex"].ToString();
                m.UserTel = dt["UserTel"].ToString();
                m.MemlevelName = dt["MemlevelName"].ToString();
               
                return m;
            
        }
        /// <summary>
        /// 根据借款编号查询积分信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public IntegralModel Get_OnlyIntegral(int ID)
        {
            SqlParameter[] Paramter = { new SqlParameter("@MemLoanId", SqlDbType.Int) };
            Paramter[0].Value = ID;
            SqlDataReader dt = DbHelperSQL.RunProcedure("Proc_IntegralSelect", Paramter);
            dt.Read();
            IntegralModel m = new IntegralModel();
            if (dt.Read() == false)
            {
                return m;
            }
            else
            {
                m.creditIntegral = int.Parse(dt["creditIntegral"].ToString());
                m.InComeIntegral = int.Parse(dt["InComeIntegral"].ToString());
                m.IntegralId = int.Parse(dt["IntegralId"].ToString());
                m.LendIntegral = int.Parse(dt["LendIntegral"].ToString());
                m.UserrId = int.Parse(dt["UserrId"].ToString());
                return m;
            }
        }

        /// <summary>
        /// 用户执行修改密码
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        public int UserPwdUpd(int UserId, string  UserPwd)
        {
            int i = 0;
            try
            {
                SqlParameter[] Parameter ={
                        new SqlParameter("UserId",SqlDbType.Int),
                         new SqlParameter("UserPwd",SqlDbType.VarChar)
                      };
                Parameter[0].Value = UserId;
                Parameter[1].Value = UserPwd;
                if (DbHelperSQL.RunProcedureReturnBool("PROC_UserPwdUpd", Parameter))
                {
                    i = 1;
                }
            }
            catch (Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }

        /// <summary>
        /// 用户绑定手机号
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="Phone"></param>
        /// <returns></returns>
        public int UserBinDingPhone(int UserId, string Phone)
        {
            int i = 0;
            try
            {
                SqlParameter[] Parameter ={
                                         new SqlParameter("UseriD",SqlDbType.Int),
                                         new SqlParameter("Phone",SqlDbType.VarChar)
                                         };
                Parameter[0].Value = UserId;
                 Parameter[1].Value = Phone;
                 if (DbHelperSQL.RunProcedureReturnBool("PROC_UserBinDingPhone", Parameter))
                 {
                     i = 1;
                 }
            }
            catch (Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }
        /// <summary>
        /// 用户绑定邮箱验证
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="Email"></param>
        /// <returns></returns>
        public int UserBinDingEmail(int UserId, string Email)
        {
            int i = 0;
            try
            {
                SqlParameter[] Parameter ={
                                         new SqlParameter("UserId",SqlDbType.Int),
                                         new SqlParameter("Email",SqlDbType.VarChar)
                                         };
                Parameter[0].Value = UserId;
                Parameter[1].Value = Email;
                if (DbHelperSQL.RunProcedureReturnBool("PROCPROC_UserBinDingEmail", Parameter))
                {
                    i = 1;
                }
            }
            catch (Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }
        /// <summary>
        /// 用户绑定银行卡信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="BankCard"></param>
        /// <returns></returns>
        public int UserBinDingBankCard(int UserId, string BankCard)
        {
            int i = 0;
            try
            {
                SqlParameter[] Parameter ={
                                         new SqlParameter("UserId",SqlDbType.Int),
                                         new SqlParameter("BankCard",SqlDbType.VarChar)
                                         };
                Parameter[0].Value = UserId;
                Parameter[1].Value = BankCard;
                if (DbHelperSQL.RunProcedureReturnBool("PROC_UserBinDingBankNO", Parameter))
                {
                    i = 1;
                }
            }
            catch (Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }


        /// <summary>
        /// 修改用户支付密码
        /// </summary>
        /// <returns></returns>
        public int UserPayPwd_Upd(UserrsModel m)
        {
            int i = 0;
            try
            {
                SqlParameter[] par ={
                                   new SqlParameter("@UserId",SqlDbType.Int),
                                   new SqlParameter("@PaymentPwd",SqlDbType.Int)
                                   };
                par[0].Value = m.UserId;
                par[1].Value = m.PaymentPwd;
                if (DbHelperSQL.RunProcedureReturnBool("proc_UserPayPwd_Upd", par))
                {
                    i = 1;
                }
            }
            catch(Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }


        /// <summary>
        /// 修改支付密码，密码验证
        /// </summary>
        /// <returns></returns>
        public int PwdQuery(int pwd,int id)
        {
            int i=0;
            try
            {
                SqlParameter[] par ={
                               new SqlParameter("@pwd",SqlDbType.Int),
                               new SqlParameter("@id",SqlDbType.Int)
                               };
                par[0].Value = pwd;
                par[1].Value = id;

                if (Convert.ToInt32 (DbHelperSQL.RunProcedureReturnSingle("Proc_ParPwdQuery", par))>0)
                {
                    i = 1;
                }
            }
            catch(Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }
    }
}
